#include <gtest/gtest.h>
#include <thread>
#include <chrono>

// 仅用于测试访问私有成员
#define private public
#define protected public
#include "ThreadPool.h"
#undef private
#undef protected

// 设置线程池模式
TEST(ThreadPoolTest, SetMode) {
  ThreadPool pool;
  pool.setMode(PoolMode::MODE_CACHED);

  EXPECT_EQ(pool.poolMode_, PoolMode::MODE_CACHED);
}

// 设置任务队列阈值
TEST(ThreadPoolTest, SetTaskQueueThreshold) {
  ThreadPool pool;
  pool.setTaskQueMaxThreshold(256);

  EXPECT_EQ(pool.taskQueThreshHold_, 256);
}

// start() 创建并启动指定数量的线程
TEST(ThreadPoolTest, StartCreatesThreads) {
  ThreadPool pool;
  constexpr std::size_t kThreads = 2;

  pool.start(kThreads);

  ASSERT_EQ(pool.threads_.size(), kThreads);
  for (auto &thPtr : pool.threads_) {
    ASSERT_TRUE(thPtr);
  }

  // 稍微等待线程体执行完成
  std::this_thread::sleep_for(std::chrono::milliseconds(20));

  // 手动 join，避免线程对象泄漏
  for (auto &thPtr : pool.threads_) {
    if (thPtr) {
      thPtr->join();
    }
  }
  pool.threads_.clear();
}
